package mine;

import java.awt.Color;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.border.LineBorder;

public class MineLabel extends JLabel {

	private static final long serialVersionUID = 1L;

	public static final ImageIcon zone = new ImageIcon(
			MineLabel.class.getResource("1.png"));
	public static final ImageIcon zone111 = new ImageIcon(
			MineLabel.class.getResource("111111.png"));
	public static final ImageIcon one = new ImageIcon(
			MineLabel.class.getResource("5.png"));
	public static final ImageIcon two = new ImageIcon(
			MineLabel.class.getResource("4.png"));
	public static final ImageIcon three = new ImageIcon(
			MineLabel.class.getResource("8.png"));
	public static final ImageIcon four = new ImageIcon(
			MineLabel.class.getResource("7.png"));
	public static final ImageIcon five = new ImageIcon(
			MineLabel.class.getResource("10.png"));
	public static final ImageIcon six = new ImageIcon(
			MineLabel.class.getResource("13.png"));

	public static final ImageIcon seven = new ImageIcon(
			MineLabel.class.getResource("91.png"));
	public static final ImageIcon eight = new ImageIcon(
			MineLabel.class.getResource("88.png"));
	public static final ImageIcon fl = new ImageIcon(
			MineLabel.class.getResource("2.png"));
	public static final ImageIcon flag = new ImageIcon(
			MineLabel.class.getResource("9.png"));

	public static final ImageIcon right = new ImageIcon(
			MineLabel.class.getResource("2.png"));
	public static final ImageIcon error = new ImageIcon(
			MineLabel.class.getResource("12.png"));
	public static final ImageIcon change = new ImageIcon(
			MineLabel.class.getResource("14.png"));
	public static final ImageIcon now = new ImageIcon(
			MineLabel.class.getResource("11.png"));
	public static final ImageIcon m = new ImageIcon(
			MineLabel.class.getResource("3.png"));
	int i;
	int j;
	private boolean connriglt;
	private boolean connleft;

	public MineLabel(final Controller controller) {
		setBorder(new LineBorder(new Color(30, 10, 180)));
		setHorizontalAlignment(JLabel.CENTER);
		addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				if ((!connriglt) && e.getButton() == 1) {
					controller.click(i, j);
				}
				if ((!connleft) && e.getButton() == 3) {
					controller.addFlag(i, j);
				}
				if (connriglt && e.getButton() == 1) {
					controller.seesee(i, j);
				}
				if (connleft && e.getButton() == 3) {
					controller.seesee(i, j);
				}
			}

			@Override
			public void mousePressed(MouseEvent e) {
				if (e.getButton() == 1) {
					connleft = true;
				}
				if (e.getButton() == 3) {
					connriglt = true;
				}

			}

			@Override
			public void mouseReleased(MouseEvent e) {
				if (e.getButton() == 1) {
					connleft = false;
				}
				if (e.getButton() == 3) {
					connriglt = false;
				}
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				controller.addLight(i, j);
			}

			@Override
			public void mouseExited(MouseEvent e) {
				controller.cutLight(i, j);
			}
			
		});

	}

	public void setmine(String str) {
		String s = str;
		setIcon(zone);
		if (!s.equals("@") && !s.equals("+")&&!s.equals("@") && !s.equals("++") && !s.equals("*")
				&& !s.equals("$") && !s.equals("#") && !s.equals("&")
				&& !s.equals("%")) {
			switch (Integer.parseInt(s)) {
			case 0:
				setIcon(null);
				break;
			case 1:
				setIcon(one);
				break;
			case 2:
				setIcon(two);
				break;
			case 3:
				setIcon(three);
				break;
			case 4:
				setIcon(four);
				break;
			case 5:
				setIcon(five);
				break;
			case 6:
				setIcon(six);
				break;
			case 7:
				setIcon(seven);
				break;
			case 8:
				setIcon(eight);
				break;
			default:
				break;
			}
		}
		if(s.equals("++")){
			setIcon(zone111);
		}
		if (s.equals("$")) {// 正确
			setIcon(right);
		}
		if (s.equals("#")) {// 错误
			setIcon(error);
		}
		if (s.equals("*")) {// 旗子
			setIcon(flag);
		}
		if (s.equals("@")) {// 雷
			setIcon(m);
		}
		if (s.equals("&")) {// 触雷
			setIcon(now);
		}
		if (s.equals("%")) {// 问号
			setIcon(change);
		}
	}

	public MineLabel(int i, int j, Controller controller) {
		this(controller);
		this.i = i;
		this.j = j;
	}
}
